<template>
  <Main>
    <PageHeader class="header-wrapper" text="Discovery">
      <!-- <van-icon class-prefix="custom-iconfont" @click="handleGotoFavorite" name="folder1" /> -->
      <!-- <van-icon name="comment-o" @click="handleGotoFavorite" /> -->
    </PageHeader>

    <div class="category-list variables-wrapper">
      <div class="category-item" v-for="(category, $index) in categoryList" :key="$index">
        <div class="category-name-rapper">
          <h3 class="category-name">{{ category.category }}</h3>
          <div class="category-next" @click="handleGotoViewAll(category)">
            <span>View all >></span>
          </div>
        </div>
        <div class="category-data-wrapper">
          <div class="book-item" v-for="(book, i) in category.rows" :key="i" @click="gotoBookDetail(book)">
            <div class="book-cover-wrapper">
              <img v-if="book.avatar" class="book-cover" :src="book.avatar | handleImageUrl" alt="" @error="onImgError">
              <van-icon class="book-cover-default" v-else name="photo" />
              <span v-if="book.tag" class="book-tag">{{ book.tag }}</span>
            </div>
            <p class="book-name">{{ book.name }}</p>
          </div>
          <div class="book-item placeholder"></div>
        </div>
      </div>
    </div>
    <PageFooter />
  </Main>
</template>

<script>
import Main from '@/views/layouts/index.vue'
import PageHeader from '@/components/page-header.vue'
import PageFooter from '@/components/page-footer.vue'
import { getDiscoveryList } from '@/api'

export default {
  name: 'Discovery',
  components: {
    Main,
    PageHeader,
    PageFooter
  },

  created() {
    this.getBookList()
  },

  data() {
    return {
      categoryList: []
    }
  },

  methods: {
    onImgError(e) {
      e.target.className += ' error'
    },
    getBookList() {
      getDiscoveryList()
        .then(res => {
          console.log(res)
          const { data } = res
          this.categoryList = data
        })
    },
    handleGotoFavorite() {
      this.$router.push({
        name: 'Favorite'
      })
    },
    handleGotoViewAll(item) {
      this.$router.push({
        name: 'ViewAll',
        query: {
          category: item.category
        }
      })
    },
    gotoBookDetail(book) {
      this.$router.push({
        name: 'Book',
        query: {
          id: book.bookid
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.header-wrapper {
  .custom-iconfont {
    font-size: 24px;
  }
}

.category-item {
  padding: 0 12px;
  .category-name-rapper {
    display: flex;
    font-size: 14px;
    justify-content: space-between;
    align-items: center;
    padding: 14px 0;

    .category-name {
      margin: 0;
      padding: 0;
      font-weight: normal;
    }
    .category-next {
      color: #a9a7a7;
    }
    .van-icon {
      font-size: 12px;
    }
  }

  .category-data-wrapper {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;

    .book-item {
      width: 32%;
      .book-name {
        width: 100%;
        padding-top: 6px;
        padding-bottom: 10px;
        word-break: normal;
        font-weight: bold;
        font-size: 14px;
      }

      .book-cover-wrapper {
        position: relative;
        display: flex;
        align-items: center;
        width: 100%;
        height: 140px;
        background-color: #eee;
        border-radius: 6px;
        overflow: hidden;

        .book-cover {
          // width: auto;
          // height: auto;
          width: 100%;
          height: 100%;
          object-fit: cover;
          margin: 0 auto;
          max-width: 100%;
          max-height: 100%;
          box-sizing: border-box;

          &.error,
          &[src=""],
          &:not([src]) {
            opacity: 0;
          }
        }

        .book-cover-default {
          width: 100%;
          text-align: center;
          font-size: 50px;
          color: #ddd;
        }
        .book-tag {
          position: absolute;
          top: 0;
          right: 0;
          color: #fff;
          padding: 2px 6px;
          font-size: 12px;
          border-radius: 4px;
          background-color: rgba(0, 0, 0, .5);
        }
      }
    }
  }
}
</style>
